#include <STC12C5A60S2.H>
#include "Delay.h"
#include "LCD12864.h"
#define DataPort P0
sbit RS=P2^4;
sbit RW=P2^5;
sbit E=P2^6;
sbit RES=P2^3;
sbit PSB=P2^1;

void LCD_Init()
{
	DelayMs(40);
	Check_Busy();
	PSB=1;
	RES=0;
	RES=1;
	Write_Com(0x30);
	DelayUs2x(50);
	Write_Com(0x30);
	DelayUs2x(20);
	Write_Com(0x0c);

} 

void Check_Busy()
{
	RS=0;
	RW=1;
	E=1;
	DataPort=0xff;
	while((DataPort&0x80)==0x80);
	E=0;

}
void Write_Com(unsigned char Com)
{
	Check_Busy();
	RS=0;
	RW=0;
	E=1;
	DataPort=Com;
	E=0;
}

void Write_Data(unsigned char Data)
{
	Check_Busy();
	RS=1;
	RW=0;
	E=1;
	DataPort=Data;
	DelayUs2x(5);
	E=0;
	DelayUs2x(5);
}

void LCD_PutString(unsigned char x,unsigned char y,unsigned char* s)
{
	switch(x)
	{
		case 0:Write_Com(0x80+y);break;
		case 1:Write_Com(0x90+y);break;
		case 2:Write_Com(0x88+y);break;
		case 3:Write_Com(0x98+y);break;
		default:break;
	
	}
	while(*s)
	{
		Write_Data(*s++);
		DelayUs2x(50);
	}

}
void ClrScreen()
{
	Write_Com(0x01);
	DelayMs(15);
}
unsigned int Pow(unsigned char x,unsigned char y)
{
	unsigned int Result=1;
	while(y--)
	{	
		Result=Result*x;
	}
	return Result;

}
void LCD_ShowNum(unsigned char x,unsigned char y,unsigned int Num,unsigned char Length)
{
	unsigned char i;
	switch(x)
	{
		case 0:Write_Com(0x80+y);break;
		case 1:Write_Com(0x90+y);break;
		case 2:Write_Com(0x88+y);break;
		case 3:Write_Com(0x98+y);break;
		default:break;
	}
	for(i=0;i<Length;i++)
	{
		Write_Data(Num/Pow(10,Length-i-1)%10+'0');
	}
}
void Scorlling(unsigned char*Str1,unsigned char*Str2)
{
	unsigned char i;
	Write_Com(0x34);Write_Com(0x03);
	for(i=0;i<64;i++)
	{
		DelayMs(98);
	     Write_Com(0x40+i);
		if(i==32)
		{
			Write_Com(0x30);
			LCD_PutString(0,0,Str1);
			LCD_PutString(1,0,Str2);
		}
	}
}
	